package com.cb.orderservice.service.impl;

import com.cb.orderservice.client.EduClient;
import com.cb.orderservice.client.UcenterClient;
import com.cb.orderservice.entity.TOrder;
import com.cb.orderservice.mapper.TOrderMapper;
import com.cb.orderservice.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.orderservice.utils.OrderNoUtil;
import com.cb.utils.ordervo.CourseWebOrderVo;
import com.cb.utils.ordervo.UcenterMemberOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Cebb
 * @since 2020-06-29
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    private final EduClient eduClient;

    private final UcenterClient ucenterClient;

    @Autowired
    public TOrderServiceImpl(EduClient eduClient, UcenterClient ucenterClient) {
        this.eduClient = eduClient;
        this.ucenterClient = ucenterClient;
    }

    @Override
    public String createOrder(String courseId, String memberId) {
        // 通过feign拿着用户id, 获取用户信息
        UcenterMemberOrderVo userInfo = ucenterClient.getUserInfoOrder(memberId);

        // 通过远程调用根据课程id获取课信息
        CourseWebOrderVo courseInfo = eduClient.getCourseInfoOrder(courseId);

        //创建订单
        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfo.getTitle());
        order.setCourseCover(courseInfo.getCover());
        order.setTeacherName(courseInfo.getTeacherName());
        order.setTotalFee(courseInfo.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfo.getMobile());
        order.setNickname(userInfo.getNickname());
        // 支付状态, 未支付:0 , 支付:1
        order.setStatus(0);
        // 支付类型, 微信:1 , 支付宝:2
        order.setPayType(1);
        baseMapper.insert(order);

        return order.getOrderNo();
    }

}
